﻿/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#ifndef __XOSKIT_XOS_CORE_XOS_STL_SRC_DB_DB_RECORED_USE_H__
#define __XOSKIT_XOS_CORE_XOS_STL_SRC_DB_DB_RECORED_USE_H__

namespace xos_stl
{

    template< class CHILD >
    class db_record_use
    {
    public:
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // 
        db_record_use()
        {
        }
        ~db_record_use()
        {
        }
    public:
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // 
        int set_use( int nIndex, bool bUse )
        {
            int ret = 0;

            CHILD * pThis = static_cast<CHILD*>( this );

            bool * pFields = 0;
            int nNum = 0;

            if( 0 == ret )
            {
                ret = pThis->get_uses( &pFields, &nNum );
            }

            if( ( 0 == ret ) && ( nIndex >= nNum ) )
            {
                ret = 1;
            }

            if( 0 == ret )
            {
                pFields[nIndex] = bUse;
            }

            return ret;
        }
        int set_use()
        {
            int ret = 0;

            CHILD * pThis = static_cast<CHILD*>( this );

            bool * pFields = 0;
            int nNum = 0;

            if( 0 == ret )
            {
                ret = pThis->get_uses( &pFields, &nNum );
            }

            for( int i = 0; ( 0 == ret ) && ( i < nNum ); ++i )
            {
                pFields[i] = true;
            }

            return ret;
        }
        bool get_use( int nIndex )
        {
            int ret = 0;

            CHILD * pThis = static_cast<CHILD*>( this );

            bool * pFields = 0;
            bool bRet = false;
            int nNum = 0;

            if( 0 == ret )
            {
                ret = pThis->get_uses( &pFields, &nNum );
            }

            if( ( 0 == ret ) && ( nIndex >= nNum ) )
            {
                ret = 1;
            }

            if( 0 == ret )
            {
                bRet = pFields[nIndex];
            }

            return bRet;
        }
        int clear_use()
        {
            int ret = 0;

            CHILD * pThis = static_cast<CHILD*>( this );

            bool * pFields = 0;
            int nNum = 0;

            if( 0 == ret )
            {
                ret = pThis->get_uses( &pFields, &nNum );
            }

            for( int i = 0; ( 0 == ret ) && ( i < nNum ); ++i )
            {
                pFields[i] = false;
            }

            return ret;
        }
    };

} // xos_stl

#endif // __XOSKIT_XOS_CORE_XOS_STL_SRC_DB_DB_RECORED_USE_H__
